#/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
Copyright (C) 2010 dbzhang800@gmail.com
All rights reserved.

2010.07.21-2010.07.XX
''' 

import sys
import os
import platform
import shutil
import subprocess
import re
from munro_common import *

def callMunroSoelens(filename=""):
    '''call external program aber.exe. if filename was not expty, give the file to aber.exe'''
    callMunroPrograms("soelens", filename)

class SoelensDatFile:
    u"""aber dat  文件的封装类，注意，
    1. dat 文件中的关键字 大小写 不区分
    2. 关键词前可以加空格或Tab
    3. 文件内允许加 // 引入注释
    4. 允许会出现空行
    """
    def __init__(self, filename):
        if not filename.lower().endswith('.dat'):
            filename += '.dat'
        self.filename = filename
        self.content = []
        self.valid = False
        if os.path.exists(filename):
            with open(filename, 'rU') as f:
                self.content = f.readlines()
        blanks = []
        for row in range(len(self.content)):
            line = self.content[row]
            if line.strip() == "":
                blanks.append(row)
        self.blank_row_numbers = []
        if len(blanks) > 5:
            t1 = blanks[1]-blanks[0]
            t2 = blanks[2]-blanks[1]
            t3 = blanks[3]-blanks[2]
            t4 = blanks[4]-blanks[3]
            if t1==t2 and t2==t3 and t3==t4:
                self.valid = True
                self.blank_row_numbers = blanks
        
    def save(self, filename=None):
        if not filename:
            filename = self.filename
        if not filename.lower().endswith('.dat'):
            filename += '.dat'
        with open(filename, 'w') as f:
            f.writelines(self.content)
    def getMeshTableRect(self):
        if self.valid:
            items = self.content[self.blank_row_numbers[0]+2].split()
            l = float(items[1])
            r = float(items[-1])
            items = self.content[self.blank_row_numbers[1]+2].split()
            t = float(items[1])
            items = self.content[self.blank_row_numbers[2]-1].split()
            b = float(items[1])
            return MunroMeshTableRect(l, r, t, b)
        else:
            return None

if __name__ == "__main__":
    setMunroPath(r"E:\opee pack-0.3.0.1\data\munro")
    setMunroPath(u"F:\\整理\\ziliao\\linux\\work\\munro\\munro")
    callMunroSoelens()
    dat = SoelensDatFile("soelens1.dat")
    print dat.getMeshTableRect()



